package com.zhongxi.guoyuan.utils;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.coinStore.AttrItemBean;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import static com.umeng.socialize.utils.DeviceConfig.context;
import static com.zhongxi.guoyuan.R.id.tv;
import static com.zhongxi.guoyuan.R.string.get;

/**
 * Created by Administrator on 2017/6/20.
 */

public class DataUtil {


    private static final int DEFAULT_DIV_SCALE = 2;

    /**
     * @param itemAttrList
     * @param attrs        //可选的属性
     * @param item
     * @param position
     * @return
     */
    public static List<List<AttrItemBean>> updataByAttrId(List<List<AttrItemBean>> itemAttrList, List<String> attrs, int item, int position) {
//        for (int i = 0; i < attrs.size(); i++) {
//            if (attrId.equals(attrs.get(i))) {
//                attrs.remove(i);
//                i--;
//            }
//        }

        String[] canSelectAttrList = attrs.toArray(new String[attrs.size()]);

        for (int i = 0; i < itemAttrList.size(); i++) {
            if (i == item) {
                List<AttrItemBean> attrItemBeens = itemAttrList.get(i);
                for (int j = 0; j < attrItemBeens.size(); j++) {
                    if (j == position) {
                        AttrItemBean attrItemBean = attrItemBeens.get(j);
                        attrItemBean.setStates("0");
                    }
                }
            } else {
                List<AttrItemBean> attrItemBeens = itemAttrList.get(i);
                for (int k = 0; k < attrItemBeens.size(); k++) {
                    String attrId1 = attrItemBeens.get(k).getAttrId();
                    // int i1 = Arrays.binarySearch(canSelectAttrList, attrId1);
                    boolean contains = Arrays.asList(canSelectAttrList).contains(attrId1);
                    if (contains) {
                        AttrItemBean attrItemBean = attrItemBeens.get(k);
                        attrItemBean.setStates("1");
                    } else {
                        AttrItemBean attrItemBean = attrItemBeens.get(k);
                        attrItemBean.setStates("2");
                    }
                }
            }
        }
        return itemAttrList;
    }

    public static List<List<AttrItemBean>> updataAdapter(List<List<AttrItemBean>> itemAttrList) {
        for (int i = 0; i < itemAttrList.size(); i++) {
            List<AttrItemBean> attrItemBeens = itemAttrList.get(i);
            for (int j = 0; j < attrItemBeens.size(); j++) {
                AttrItemBean attrItemBean = attrItemBeens.get(j);
                attrItemBean.setStates("1");
            }

        }
        return itemAttrList;
    }

    public static List<List<AttrItemBean>> updataInfo(List<List<AttrItemBean>> itemAttrList, ArrayList<List<String>> allCanSelectAttrsList, Map<String, AttrItemBean> selectAttrMap) {
        //去重,合并
        Set set = new HashSet();
        ArrayList<String> canSelectCollection = new ArrayList<>();
        for (List<String> canSelectList : allCanSelectAttrsList) {
            for (String canSelect : canSelectList) {
                if (set.add(canSelect)) {
                    canSelectCollection.add(canSelect);
                }
            }
        }

        //已选的属性id
        ArrayList<String> selectAttrIdList = new ArrayList<>();
        for (String key : selectAttrMap.keySet()) {
            AttrItemBean s = selectAttrMap.get(key);
            selectAttrIdList.add(s.getAttrId());
        }
        String[] selectAttrsMost = selectAttrIdList.toArray(new String[selectAttrIdList.size()]);

        //从可选的属性id集合中去除已选的属性id
        for (int i = 0; i < canSelectCollection.size(); i++) {
            String s = canSelectCollection.get(i);
            boolean contains = Arrays.asList(selectAttrsMost).contains(s);
            if (contains) {
                canSelectCollection.remove(i);
                i--;
            }
        }
        String[] canSelectAttrMost = canSelectCollection.toArray(new String[canSelectCollection.size()]);

        for (int i = 0; i < itemAttrList.size(); i++) {
            List<AttrItemBean> attrItemBeens = itemAttrList.get(i);
            for (int j = 0; j < attrItemBeens.size(); j++) {
                String attrId = attrItemBeens.get(j).getAttrId();
                boolean contains = Arrays.asList(selectAttrsMost).contains(attrId);
                if (contains) {
                    AttrItemBean attrItemBean = attrItemBeens.get(j);
                    attrItemBean.setStates("0");
                    continue;
                }
                boolean contains1 = Arrays.asList(canSelectAttrMost).contains(attrId);
                if (contains1) {
                    AttrItemBean attrItemBean = attrItemBeens.get(j);
                    attrItemBean.setStates("1");
                    continue;
                }
                AttrItemBean attrItemBean = attrItemBeens.get(j);
                attrItemBean.setStates("2");
            }
        }

        return itemAttrList;
    }

    public static List<List<AttrItemBean>> updataInfoUpgrade(List<List<AttrItemBean>> itemAttrList, List<String> canSelectCollection, Map<String, AttrItemBean> selectAttrMap) {

        //已选的属性id
        ArrayList<String> selectAttrIdList = new ArrayList<>();
        for (String key : selectAttrMap.keySet()) {
            AttrItemBean s = selectAttrMap.get(key);
            selectAttrIdList.add(s.getAttrId());
        }
        String[] selectAttrsMost = selectAttrIdList.toArray(new String[selectAttrIdList.size()]);

        //从可选的属性id集合中去除已选的属性id
        for (int i = 0; i < canSelectCollection.size(); i++) {
            String s = canSelectCollection.get(i);
            boolean contains = Arrays.asList(selectAttrsMost).contains(s);
            if (contains) {
                canSelectCollection.remove(i);
                i--;
            }
        }
        String[] canSelectAttrMost = canSelectCollection.toArray(new String[canSelectCollection.size()]);

        for (int i = 0; i < itemAttrList.size(); i++) {
            List<AttrItemBean> attrItemBeens = itemAttrList.get(i);
            for (int j = 0; j < attrItemBeens.size(); j++) {
                String attrId = attrItemBeens.get(j).getAttrId();
                boolean contains = Arrays.asList(selectAttrsMost).contains(attrId);
                if (contains) {
                    AttrItemBean attrItemBean = attrItemBeens.get(j);
                    attrItemBean.setStates("0");
                    continue;
                }
                boolean contains1 = Arrays.asList(canSelectAttrMost).contains(attrId);
                if (contains1) {
                    AttrItemBean attrItemBean = attrItemBeens.get(j);
                    attrItemBean.setStates("1");
                    continue;
                }
                AttrItemBean attrItemBean = attrItemBeens.get(j);
                attrItemBean.setStates("2");
            }
        }

        return itemAttrList;
    }


    /**
     * 判断两个集合是否相等
     *
     * @param list1
     * @param list2
     * @return
     */
    public static boolean equalList(List list1, List list2) {
        if (list1.size() != list2.size())
            return false;
        if (list2.containsAll(list1))
            return true;
        return false;
    }

    /**
     * 获取两个集合中相同的元素
     *
     * @param allCanSelectAttrsLists
     * @param canSelectAttrList
     * @return
     */
    public static ArrayList<String> sameElementInTwoList(ArrayList<String> allCanSelectAttrsLists, ArrayList<String> canSelectAttrList) {
        ArrayList<String> copyCanSelectAttrList = new ArrayList<>(canSelectAttrList);
        canSelectAttrList.removeAll(allCanSelectAttrsLists);  //获取两个数组不同的元素
        copyCanSelectAttrList.removeAll(canSelectAttrList);//获取两个数组相同的元素
        return copyCanSelectAttrList;
    }

    /**
     * 使用BigDecimal，保留小数点后两位
     *
     * @param value
     * @return
     */
    public static String formatData(String value) {

        BigDecimal bd = new BigDecimal(value);

        return bd.setScale(2, RoundingMode.HALF_UP).toString();
    }

    /**
     * 使用BigDecimal，保留小数点后两位
     *
     * @param value
     * @return
     */
    public static int intValue(String value) {

        BigDecimal bd = new BigDecimal(value);
        return bd.intValue();
    }

    /**
     * 使用BigDecimal，保留小数点后两位
     *
     * @param value
     * @return
     */
    public static String formatDataFivePoint(String value) {

        BigDecimal bd = new BigDecimal(value);

        return bd.setScale(5, RoundingMode.HALF_UP).toString();
    }

    public static String formatDataSixPoint(String value) {

        BigDecimal bd = new BigDecimal(value);

        return bd.setScale(6, RoundingMode.HALF_UP).toString();
    }

    /**
     * 提供精确的减法运算
     *
     * @param v1
     * @param v2
     * @return 两个参数数学差，以字符串格式返回
     */
    public static String subtract(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.subtract(b2).toString();
    }

    /**
     * 是否大于零
     *
     * @param v1
     * @return
     */
    public static boolean moreThanZero(String v1) {
        BigDecimal b = new BigDecimal(v1);
        int i = b.compareTo(BigDecimal.ZERO);
        if (i == 1) {
            return true;
        }
        return false;
    }

    /**
     * 是否等于零
     *
     * @param v1
     * @return
     */
    public static boolean equalZero(String v1) {
        BigDecimal b = new BigDecimal(v1);
        int i = b.compareTo(BigDecimal.ZERO);
        if (i == 0) {  //等于   -1 小于
            return true;
        }
        return false;
    }

    /**
     * 是否v1大于等于v2
     *
     * @param v1
     * @param v2
     * @return
     */
    public static boolean moreThanOther(String v1, String v2) {
        String subtract = subtract(v1, v2);
        if (moreThanZero(subtract) || equalZero(subtract)) {
            return true;
        }
        return false;
    }

    /**
     * 是否v1大于v2
     *
     * @param v1
     * @param v2
     * @return
     */
    public static boolean moreThanOtherNoEqual(String v1, String v2) {
        String subtract = subtract(v1, v2);
        if (moreThanZero(subtract)) {
            return true;
        }
        return false;
    }

    /**
     * 提供精确的加法运算
     *
     * @param v1
     * @param v2
     * @return 两个参数数学加和，以字符串格式返回
     */

    public static String add(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.add(b2).toString();
    }


    /**
     * 提供（相对）精确的除法运算。当发生除不尽的情况时，由scale参数指
     * <p>
     * 定精度，以后的数字四舍五入。舍入模式采用用户指定舍入模式
     *
     * @param v1
     * @param v2
     * @param scale 表示需要精确到小数点以后几位
     * @return 两个参数的商，以字符串格式返回
     */

    public static String divide(String v1, String v2, int scale) {

        if (scale < 0) {

            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }

        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);

        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString();

    }


    /**
     * 提供（相对）精确的乘法运算。当发生除不尽的情况时，由scale参数指
     * <p>
     * 定精度，以后的数字四舍五入。舍入模式采用用户指定舍入模式
     *
     * @param v1
     * @param v2
     * @param scale
     * @return 两个参数的积，以字符串格式返回
     */

    public static String multiplys(String v1, String v2, int scale) {

        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        String s = b1.multiply(b2).toString();
        BigDecimal num = new BigDecimal(s);
        return num.setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 被整除
     *
     * @return
     */
    public static boolean divisible(String v1, String v2) {
        String divide = divide(v1, v2, DEFAULT_DIV_SCALE);
        int i = divide.lastIndexOf('.');
        String substring = divide.substring(i + 1, divide.length());
        int i1 = Integer.parseInt(substring);
        if (i1 == 0) {
            return true;
        }
        return false;
    }

    /**
     * 找到该元素在该集合中的下标
     *
     * @param list
     * @param str
     * @return
     */
    public static int indexInList(List<String> list, String str) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(str)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 获取数组中的最大值(int)
     *
     * @param list
     * @return
     */
    public static int maxInteger(List<Integer> list) {
        int max = list.get(0).intValue();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).intValue() > max) {
                max = list.get(i).intValue();
            }
        }
        return max;
    }

    /**
     * 获取数组中的最大值(String)
     *
     * @param list
     * @return
     */
    public static String maxString(List<String> list) {
        String max = list.get(0);
        for (int i = 0; i < list.size(); i++) {
            String subtract = subtract(list.get(i), max);
            if (moreThanZero(subtract)) {
                max = list.get(i);
            }
        }
        return max;
    }

    /**
     * 被选中
     *
     * @param context
     * @param ll
     * @param tv1
     * @param tv2
     */
    public static void isSelect(Context context, LinearLayout ll, TextView tv1, TextView tv2) {
        ll.setBackgroundResource(R.drawable.shape2);  //R.drawable.bg_red_solid_round
        tv1.setTextColor(context.getResources().getColor(R.color.white));
        tv2.setTextColor(context.getResources().getColor(R.color.white));
    }

    /**
     * 可选
     *
     * @param context
     * @param ll
     * @param tv1
     * @param tv2
     */
    public static void canSelect(Context context, LinearLayout ll, TextView tv1, TextView tv2) {
        ll.setBackgroundResource(R.drawable.shape1);
        tv1.setTextColor(context.getResources().getColor(R.color.bg_yield));
        tv2.setTextColor(context.getResources().getColor(R.color.bg_yield));
    }

    /**
     * 不可选
     *
     * @param context
     * @param ll
     * @param tv1
     * @param tv2
     */
    public static void noSelect(Context context, LinearLayout ll, TextView tv1, TextView tv2) {
        ll.setBackgroundResource(R.drawable.shape3);
        tv1.setTextColor(context.getResources().getColor(R.color.bottom_color));
        tv2.setTextColor(context.getResources().getColor(R.color.bottom_color));
    }

    /**
     * 判断一个字符串是否为浮点数
     *
     * @param str
     * @return
     */
    public static boolean isDouble(String str) {
        if (null == str || "".equals(str)) {
            return false;
        }
        return str.matches("[\\d]+\\.[\\d]+");
    }

    /**
     * 判断一个字符串是否为json
     *
     * @param json
     * @return
     */

    public static boolean isGoodJson(String json) {
        try {
            new JsonParser().parse(json);
            return true;
        } catch (JsonParseException e) {
            System.out.println("bad json: " + json);
            return false;
        }
    }

}
